package com.yingwei.mining.appletversion;

import java.awt.*;
import java.awt.image.ImageObserver;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.HashMap;

/**
 * User: yingwei Date: 2009-11-15 Time: 21:54:18
 */
public class Excavator extends Device implements Runnable, Drawable
{
    private static int idSeries = 0;
    private String id = "";
    private Map<Integer, Integer> schedules;
    private boolean available;
    private boolean isCoal = true;

    public Excavator(Position pos, Image img)
    {
	this.setId("" + idSeries++);
	this.setAvailable(true);
	this.setPosition(pos);
	this.setId(id);
	setImage(img);
	schedules = new LinkedHashMap<Integer, Integer>();
    }

    public void run()
    {
	;
    }

    public void draw(Graphics g, ImageObserver obsv)
    {
	g.drawImage(this.getImage(), this.getPosition().getX(), this
		.getPosition().getY(), obsv);
	g.drawString(id, this.getPosition().getX(), this.getPosition().getY());
	// System.out.println("printing schedules:" + id);
	for (Integer it : schedules.keySet())
	{
	    // System.out.println("start:" + it + " end:" + schedules.get(it) );
	}
    }

    public void write(Graphics g, ImageObserver obsv)
    {
	int i = 0;
	for (Integer it : schedules.keySet())
	{
	    g.setColor(Color.getHSBColor((float) 255, (float) 255, (float) Math
		    .random() * 255));
	    g.drawLine(this.getPosition().getX() + it / 10, this.getPosition()
		    .getY()
		    + i, this.getPosition().getX() + schedules.get(it) / 10,
		    this.getPosition().getY() + i++);
	    g
		    .drawString("start:" + it + " end:" + schedules.get(it),
			    this.getPosition().getX(), this.getPosition()
				    .getY()
				    + i++ * 10);
	}
	for (Integer it : schedules.keySet())
	{
	    for (Integer it1 : schedules.keySet())
	    {
		if (it < it1 && schedules.get(it) > it1)
		{
		    System.out.println("conflicts,id:" + id + ":" + it + ","
			    + schedules.get(it) + ";" + it1 + ","
			    + schedules.get(it1));
		}
	    }
	    g.setColor(Color.getHSBColor((float) 255, (float) 255, (float) Math
		    .random() * 255));
	    g.drawLine(this.getPosition().getX() + it / 10, this.getPosition()
		    .getY()
		    + i, this.getPosition().getX() + schedules.get(it) / 10,
		    this.getPosition().getY() + i++);
	    g
		    .drawString("start:" + it + " end:" + schedules.get(it),
			    this.getPosition().getX(), this.getPosition()
				    .getY()
				    + i++ * 10);
	}

    }

    public Map<Integer, Integer> getSchedules()
    {
	return schedules;
    }

    public void setSchedules(Map<Integer, Integer> schedules)
    {
	this.schedules = schedules;
    }

    public Integer findNextAvailableSchedule(Integer start, Integer end)
    {
	Integer length = end - start;
	boolean overlap = false;
	for (Integer st1 : schedules.keySet())
	{
	    Integer ed1 = schedules.get(st1);
	    if (start >= st1 && start <= ed1)
	    {
		overlap = true;
		start = ed1 + 1;
		break;
	    }
	    if (end >= st1 && end <= ed1)
	    {
		overlap = true;
		start = ed1 + 1;
		break;
	    }
	    if (start < st1 && end > ed1)
	    {
		overlap = true;
		start = ed1 + 1;
		break;
	    }
	}

	if (!overlap)
	{
	    return start;
	} else
	{
	    return findNextAvailableSchedule(start, start + length);
	}
    }

    public void addSchedule(Integer start, Integer end)
    {
	schedules.put(start, end);
	if (schedules.size() > 1000)
	{
	    // remove first 500 inserted data.
	    int counter = 0;
	    for (Integer st : schedules.keySet())
	    {
		if (counter++ < 500)
		{
		    schedules.remove(st);
		}
	    }
	}
    }

    public String getId()
    {
	return id;
    }

    public void setId(String id)
    {
	this.id = id;
    }

    public boolean isAvailable()
    {
	return this.available;
    }

    public void setAvailable(boolean available)
    {
	this.available = available;
    }

    public String getSchedulesAsString()
    {
	String result = "current steps:" + FrameClock.currentSteps + "\n";
	for (Integer st1 : schedules.keySet())
	{
	    Integer ed1 = schedules.get(st1);
	    result += "s:" + st1 + " e:" + ed1 + "\n";
	}
	return result;
    }

    public void info(int mx, int my)
    {
	if (mx > this.getPosition().getX()
		&& mx < this.getPosition().getX() + 5
		&& my > this.getPosition().getY()
		&& my < this.getPosition().getY() + 5)
	{
	    System.out.println("true");
	    System.out.println(this.getSchedulesAsString());
	}
    }
}
